const http = require('./http');

const appIndexURL = '/app/index';
const booksURL = '/textbooks/';
const bookConditionsURL = '/conditions/textbook';
const bookDetailURL = '/textbooks/';
const unitsURL = '/units/english/';
const wordsURL = '/words'; //单词列表
const wordDetailURL = '/words/'; //单词列表

const normalAuthURL = '/auth'; // APP登录
const authURL = '/weChatProgram/auth'; //微信三方登录
const gradesURL = '/grades/all'; // 班级列表
const schoolsURL = '/schools'; // 搜索学校列表
const uploadImageURL = '/files/images' // 上传图片
const smscodeURL = '/sms/register' // 用户注册验证码
const userBindURL = '/weChatProgram/binding' //用户绑定
const updateUserInfoURL = '/users/me' //修改用户信息
const userIntegrationsURL = '/users/me/pointLogs' //用户积分明细
const collectWordURL = '/favorites/words'; // 收藏单词
const collectTopicsURL = '/topics/favorites'; // 收藏知识点

const uncollectTopicsURL = '/topics/favorites'; //取消收藏知识点 /topics/favorites/{domainType}/{domainId}
const feedbackURL = '/feedbacks'; //问题反馈
const inviteRecordsURL = '/users/me/invitees'; //邀请列表
const practicesConditionsURL = '/conditions/practice'; //练习筛选条件
const practicesUrl = "/practices"; //练习列表
const memoryVideosURL = "/memory/videos"; //记忆法视频列表
const memoryVideoDetailURL = '/memory/videos/'; //记忆法视频详情  /memory/videos/{id}
const memoryVideoLikesURL = '/memory/videos/'; //视频点赞 /memory/videos/{id}/likes
const traningCodesListURL = '/memory/coding/list'; //右脑训练编码表
const messagesURL = '/messages'; //消息
const hotSearchURL = '/globalItems/recSearchTexts'; //热门搜索
const searchModulesURL = '/globalItems/modules'; //搜索模块
const allSearchURL = '/globalItems'; //全局搜索结果
const topicsURL = '/globalItems/topic'; //知识点
const topicDetailURL = '/topics'; //知识点详情 /topics/{domainType}/{domainId}
const unreadMsgCountsURL = '/messages/count/unread';
const studentsSaidsURL = '/reflections'; //学生感言

// 九大学科
const subjectsURL = '/subjects'; // 学科列表
const getUnitsURL = '/units/byTextbookId/'; // 单元列表
// 舒尔特
const getMyGradesURL = '/schulte/myResults/'; // 获取某模式下所有级别的成绩
const postTrainResultURL = '/schulte/myResults'; // 提交训练结果
const getMyGradeURL = '/schulte/myResult'; // 获取我的成绩
const getRankListURL = '/schulte/ranks'; // 获取排行榜
// 支付
const createOrdersURL = '/orders'; //创建支付订单
const payOrderURL = '/payments/wxProgram'; //支付
const studyBeansURL = '/payments/studyBeans';

const Api = {
  /**
   * 获取首页内容(banner、控件显示与否等)
   * 
   * @param {Function} callback 
   */
  getAppIndex(callback) {
    http.get(appIndexURL, null, (data) => {
      callback(data);
    })
  },
  /**
   *获取课本列表
   *
   * @param {Object} data
   * @param {Function} callback
   */
  getBooks(data, callback) {
    http.post(booksURL, data, (res) => {
      callback(res);
    })
  },
  /**
   *获取我的课程课本列表
   *
   * @param {Object} data
   * @param {Function} callback
   */
  getMyBooks(data, callback, value) {
    
    let defaultCondition = [{
      id: 1,
      name: "是否订阅",
      multiple: true,
      options: [{
        id: "1",
        name: "已订阅"
      }]
    }]
    let selectedConditions = data.conditions;
    data.conditions = selectedConditions.concat(defaultCondition);
    this.getBooks(data, callback);
  },
  /**
   *获取某一学科课本列表
   *
   * @param {String | Number} type
   * @param {Object} data
   * @param {Function} callback
   */
  getSubjectBooks(subjectId, data, callback) {
    let defaultCondition = [{
      "id": 5,
      "options": [{
        "id": subjectId
      }]
    }]
    let selectedConditions = data.conditions;
    data.conditions = selectedConditions.concat(defaultCondition);
    this.getBooks(data, callback);
  },
  /**
   *获取课本筛选条件
   *
   * @param {String | Number} type
   * @param {Function} callback
   */
  getBookConditions(type, callback) {
    http.get(bookConditionsURL, {
      type: type
    }, callback);
  },
  getSubjects(callback) {
    http.get(subjectsURL, null, callback);
  },
  /**
   *获取课本详情
   *
   * @param {String} id
   * @param {Function} callback
   */
  getBookDetail(id, callback) {
    let url = bookDetailURL + id;
    http.get(url, null, callback);
  },
  /**
   *获取单元列表
   *
   * @param {String} id
   * @param {Function} callback
   */
  getUnits(id, callback) {
    let url = getUnitsURL + id;
    http.get(url, null, callback);
  },
  /**
   *
   * 获取单词列表
   * @param {fuzzyName} params
   * @param {Function} callback
   */
  getWords(params, callback) {
    http.get(wordsURL, params, callback);
  },
  /**
   * 获取一个单词
   *
   * @param {String} id id: ...
   * @param {Function} callback
   */
  getWordDetail( id, callback ) {
    let url = wordDetailURL + id;
    http.get( url, null, callback )
  },
  /**
   * 获取收藏单词列表
   *
   * @param {*} callback
   */
  getCollectedWords(params, callback ) {
    params = params || {};
    params.favorite = true;
    http.get(wordsURL, params, callback);
  },
  /**
   * 三方认证登录
   *
   * @param {String} authCode wx.login 获取code
   * @param {String | Number} type 1.微信 2.qq 3.微博
   * @param {Function} callback
   */
  authLogin(params, callback) {
    http.post(authURL, params, callback);
  },
  /**
   * APP登录方式
   *
   * @param {Object} params
   * @param {Function} callback
   */
  normalLogin( params, callback ) {
    http.post( normalAuthURL, params, callback );
  },
  normalLogout( callback ) {
    http.delete( normalAuthURL, null, callback );
  },
  /**
   * 获取年级列表
   *
   * @param {Function} callback
   */
  getGrades(callback) {
    http.get(gradesURL, null, callback);
  },
  /**
   * 搜索学校列表
   *
   * @param {String} text
   * @param {Function} callback
   */
  getSchools(text, callback) {
    let params = {
      searchText: text
    }
    http.get(schoolsURL, params, callback);
  },
  /**
   * 上传图片
   *
   * @param {String | Number} type 0: 头像 1: 答案图片
   * @param {String} file
   * @param {Function} callback
   */
  uploadImage(type, path, callback) {
    let url = http.baseurl + uploadImageURL;
    console.log( url );
    wx.uploadFile({
      url: url,
      filePath: path,
      name: 'file',
      formData:{
        'type': type
      },
      success: function(res){
        var data = JSON.parse(res.data);
        callback( data );
      }
    })
  },
  /**
   * 获取用户注册验证码
   *
   * @param {Function} callback
   */
  sendSmsCode( phone, callback ) {
    let params = {
      phone: phone
    }
    http.get( smscodeURL, params, callback )
  },
  /**
   * 用户绑定
   *
   * @param {Object} params
   * @param {Function} callback
   */
  userBind( params, callback ) {
    http.post( userBindURL, params, callback );
  },
  /**
   * 修改用户信息
   *
   * @param {Object} params
   * @param {Function} callback
   */
  updateUserInfo( params, callback ) {
    http.put( updateUserInfoURL, params, callback )
  },
  /**
   * 我的积分
   *
   * @param {Object} params 参数：pageNo, pageSize
   * @param {Function} callback
   */
  getUserIntegrations( params, callback ) {
    http.get( userIntegrationsURL, params, callback );
  },
  /**
   * 知识点详情
   *
   * @param {String} id 实体id
   * @param {String} type 实体类型 = ['TEXTBOOK', 'PRACTICE', 'MEMORY_LESSON', 'USER', 'WORD']
   * @param {*} callback
   */
  topicDetail( id, type, callback ) {
    let url = topicDetailURL + '/' + type + '/' + id;
    http.get(url, null, callback);
  },
  /**
   * 收藏知识点
   *
   * @param {String} id 实体id
   * @param {String} type 实体类型 = ['TEXTBOOK', 'PRACTICE', 'MEMORY_LESSON', 'USER', 'WORD']
   * @param {*} callback
   */
  collectTopics( id, type, callback ) {
    let params = {
      domainId: id,
      domainType: type
    }
    http.post(collectTopicsURL, params, callback);
  },
  getCollectedTopics(params, callback ) {
    http.get(collectTopicsURL, params, callback);
  },
  /**
   * 取消收藏知识点
   *
   * @param {String} id 实体id
   * @param {String} type 实体类型 = ['TEXTBOOK', 'PRACTICE', 'MEMORY_LESSON', 'USER', 'WORD']
   * @param {*} callback
   */
  uncollectTopics(id, type, callback) {
    let url = uncollectTopicsURL + '/' + type + '/' + id;
    http.delete(url, null, callback);
  },
  /**
   * 收藏单词
   *
   * @param {*} wordId
   */
  collectWord( wordId, callback ) {
    let params = {
      wordId: wordId
    }
    http.post( collectWordURL, params, callback );
  },
  /**
   * 单词取消收藏
   *
   * @param {String} wordId
   * @param {Function} callback
   */
  uncollectWord( wordId, callback ) {
    let url = collectWordURL + '/' + wordId;
    http.delete( url, null, callback );
  },
  /**
   * 问题反馈
   * 
   * @param {Object} params {content: '', title: ''}
   * @param {Function} callback
   */
  feedback( params, callback ) {
    http.post( feedbackURL, params, callback )
  },
  /**
   * 邀请记录
   *
   * @param {Object} params
   * @param {Function} callback
   */
  inviteRecords(params, callback) {
    
    http.get( inviteRecordsURL, params, callback );
  },
  /**
   * 练习
   *
   * @param {Object} params
   * @param {Function} callback
   */
  practices( params, callback) {
    http.post( practicesUrl, params, callback );
  },
  /**
   * 练习筛选条件
   *
   * @param {Function} callback
   */
  practicesConditions( callback ) {
    http.get( practicesConditionsURL, null, callback );
  },
  /**
   * 记忆法视频列表
   *
   * @param {Function} callback
   */
  memoryVideos( params, callback ) {
    http.get( memoryVideosURL, params, callback );
  },
  /**
   * 记忆法视频详情
   *
   * @param {String} id 视频ID
   * @param {Function} callback
   */
  memoryVideoDetail( id, callback ) {
    let url = memoryVideoDetailURL + id;
    http.get( url, null, callback );
  },
  /**
   * 记忆法视频点赞
   *
   * @param {String} id
   * @param {Function} callback
   */
  memoryVideoLikes( id, callback ) {
    let url = memoryVideoLikesURL + id + "/likes";
    let params = {
      id: id
    }
    http.post( url, params, callback );
  },
  /**
   * 右脑训练表码表： type: 1 数字图形编码表 type:2 字母图形编码表
   *
   * @param {Number|String} type
   * @param {Function} callback
   */
  traningCodesList( type, callback ) {
    let params = {
      type: type
    }
    http.get(traningCodesListURL, params, callback)
  },
  /**
   * 消息
   *
   * @param {Object} params
   * @param {Function} callback
   */
  messages(params, callback) {
    http.get(messagesURL, params, callback);
  },
  /**
   * 标记消息为已读
   *
   * @param {String} id
   * @param {Function} callback
   */
  messageRead( id, callback ) {
    let url = messagesURL + '/' + id + '/read';
    http.put( url, null, callback );
  },
  /**
   * 未读消息数
   *
   * @param {Function} callback
   */
  unreadMsgCounts( callback ) {
    http.get( unreadMsgCountsURL, null, callback );
  },
  /**
   * 热门搜索
   *
   * @param {String} moduleId 模块ID
   * @param {Function} callback
   */
  hotSearch(moduleId, callback) {
    let params = {};
    if ( moduleId ) {
      params.moduleId = moduleId;
    }
    http.get( hotSearchURL, params, callback );
  },
  /**
   * 搜索模块列表
   *
   * @param {Function} callback
   */
  searchModules( callback ) {
    http.get( searchModulesURL, null, callback );
  },
  /**
   * 全局搜索
   *
   * @param {*} text
   * @param {*} callback
   */
  allSearch( text, callback ) {
    let params = {
      searchTextV2: text
    }
    http.get( allSearchURL, params, callback )
  },
  /**
   * 全局搜索联想
   *
   * @param {String} keyword
   * @param {Function} callback
   */
  allSearchAssociation( keyword, callback ) {
    let url = allSearchURL + '/' + keyword + '/associations'
    http.get( url, null, callback );
  },
  /**
   * 单模块联想
   *
   * @param {String} moduleId
   * @param {String} keyword
   * @param {Function} callback
   */
  moduleSearchAssociation(moduleId, keyword, callback) {
    // /globalItems/{moduleId}/{keyword}/associations
    let url = '/globalItems/' + moduleId + '/' + keyword + '/associations';
    http.get( url, null, callback );
  },
  /**
   * 知识点
   *
   * @param {String} value
   * @param {Function} callback
   */
  getTopics( value, callback ) {
    let params = {
      searchTextV2: value
    }
    http.get( topicsURL, params, callback );
  },
  
  /**
   * 发现：学生感言
   *
   * @param {Object} params
   * @param {Function} callback
   */
  studentsSaids( params, callback ) {
    http.get( studentsSaidsURL, params, callback );
  },
  /**
   * 课程：课程是否可打开
   *
   * @param {*} id
   * @param {*} callback
   */
  subjectAvalible( id, callback ) {
    let url = '/subjects/' + id + '/available'
    http.get( url, null, callback );
  },
  /**
   * 课程：开启课程
   *
   * @param {*} id
   * @param {*} callback
   */
  fireSubject( id, callback ) {
    let url = '/subjects/' + id;
    let params = {
      shareType: 1
    }
    http.post( url, params, callback );
  },
  /**
   * 支付：创建支付订单
   *
   * @param {Object} params
   * memoryId (string, optional): 记忆法课程id ,
   * paperId (string, optional): 试卷id ,
   * textbookId (string, optional): 课本id ,
   * type (integer, optional): 订阅类型，0课本、1记忆法、2试卷
   * @param {Function} callback
   */
  createOrders( params, callback ) {
    http.post( createOrdersURL, params, callback );
  },
  /**
   * 支付：发起支付
   *
   * @param {Object} params
   * @param {Function} callback
   */
  payOrder( params, callback ) {
    http.post( payOrderURL, params, callback );
  },
  /**
   * 舒尔特：获取模式下成绩
   *
   * @param {Number|String} mode
   * @param {Function} callback
   */
  getMyGrades( mode, callback ) {
    let url = getMyGradesURL + mode;
    http.get( url, null, callback );
  },
  /**
   * 舒尔特：提交训练结果
   *
   * @param {Object} params
   * @param {Function} callback
   */
  postTrainResult( params, callback ) {
    http.post( postTrainResultURL, params, callback );
  },
  /**
   * 舒尔特：获取我的成绩
   *
   * @param {Object} params
   * @param {Function} callback
   */
  getMyGrade( params, callback ) {
    http.get( getMyGradeURL, params, callback );
  },
  /**
   * 舒尔特：获取排行榜
   *
   * @param {Object} params
   * @param {Function} callback
   */
  getRankList( params, callback ) {
    http.get( getRankListURL, params, callback );
  },
  getVideoDetail(id, callback) {
    let url = '/memory/videos/' + id;
    http.get(url, null, callback);
  },
  getVideos(data, callback) {
    let url = '/memory/videos';
    http.get(url, data, callback);
  },
  createStudyBeansOrder(orderNo, callback) {
    let params = {
      outTradeNo: orderNo
    }
    http.post(studyBeansURL, params, callback);
  }
}

module.exports = Api;